import { App, epclass, navigate } from '../../../library/api/index'
Page({
  data: {
    no_has_class: false,
    hide_more_class: false,
    mc: [],
    rc: [],
    more_class: [],
    navMe: 'on',
    tip: 'blank'
  },
  onLoad: function(e) {
    epclass.getClasses({}, r => {
      if(r.code === 200) {
        if(r.mc.length == 0) {
          this.data.no_has_class = true
          this.setData({
            no_has_class: this.data.no_has_class
          })
        } else {
          for(var i = 0; i < r.mc.length; i++) {
            this.data.mc.push(r.mc[i])
          }
        }
        if(r.rc.length > 0) {
          for(var i = 0; i < r.rc.length; i++) {
            if(i < 2) {
              this.data.rc.push(r.rc[i])
            } else {
              this.data.more_class.push(r.rc[i])
            }
          }
        }
        if(r.rc.length <= 2) {
          this.setData({
            hide_more_class: true
          })
        }
        this.setData({
          mc: this.data.mc,
          rc: this.data.rc
        })
      }
    })
    App.jice.onLoad(e)
  },
  onReady: function() {
    App.jice.track('page', {
      pagepath: '/pages/me/epclass/index',
      pageTitle: '个人中心-我的电台',
      pageType: 'view'
    })
    App.logger({
      type: App.G.view,
      channel: App.G.channel,
      target: '个人中心-我的电台'
    })
  },
  onShow: function() {
    App.jice.track('appshow')
  },
  more: function() {
    for(var i = 0; i < this.data.more_class.length; i++) {
      this.data.rc.push(this.data.more_class[i])
    }
    this.setData({
      rc: this.data.rc,
      hide_more_class: true
    })
  },
  showWaitReviewTip: function() {
    this.setData({
      tip: 'wait_review'
    })
  },
  wait: function() {
    App.jice.track('click', {act: 'wait'})
    this.showWaitReviewTip()
  },
  closeWaitTip: function() {
    this.setData({
      tip: 'blank'
    })
  },
  review: function(event) {
    App.jice.track('click', event)
    navigate.to(`/pages/epclass/review/index?cid=${event.currentTarget.dataset.cid}&from=me`)
  },
  view: function(event) {
    App.jice.track('click', event)
    navigate.to(`/pages/epclass/view/index?cid=${event.currentTarget.dataset.cid}&from=me`)
  },
  live: function(event) {
    App.jice.track('click', event)
    navigate.to(`/pages/epclass/live/index?cid=${event.currentTarget.dataset.cid}&from=me`)
  },
  goApply: function(event) {
    App.jice.track('click', event)
    navigate.to('/pages/me/epclass/apply/index')
  },
  onShareAppMessage: function() {
    App.jice.track('onshare', {
      shareto: 'friend',
      title: '【专家学堂-美赞臣中国】与专家对话，做A+妈妈',
      path: '/pages/me/epclass/index'
    })
    return {
      title: '【专家学堂-美赞臣中国】与专家对话，做A+妈妈',
      desc: '听了这些资深专家的话，你也能成为A+妈妈哟~还不快来报名？',
      path: '/pages/me/epclass/index',
      success: function() {
        epclass.shareLogger({
          class_id: 0,
          target: '我的电台',
          action: 'appMessage'
        }, r => {
        })
      }
    }
  }
})
